#!/bin/bash

# Test script to demonstrate early termination validation issues
# This script shows concrete examples of missed code blocks due to 
# AST filtering and file pre-filtering optimizations

set -e

echo "🔍 Demonstrating Early Termination Validation Issues in Probe"
echo "============================================================="

# Ensure we're in the correct directory
cd "$(dirname "$0")/.."

# Build probe in debug mode to see optimization behavior
echo "📦 Building probe in debug mode..."
cargo build

PROBE_BIN="./target/debug/probe"
TEST_FILE="test_cases/early_termination_issues.rs"

echo ""
echo "🧪 Test Case 1: Multiline Function Signature Issues"
echo "---------------------------------------------------"
echo "Searching for 'complex_database_query' - should find the complete function including signature"

echo ""
echo "🔍 Normal search (should work):"
$PROBE_BIN search "complex_database_query" "$TEST_FILE" --max-results 1

echo ""
echo "🔍 Search targeting specific implementation lines (potential early termination issue):"
echo "Looking for 'retry_count' which is inside the function body..."
$PROBE_BIN search "retry_count" "$TEST_FILE" --max-results 1

echo "⚠️  Question: Does the result include the complete function signature from lines 10-20?"
echo "   If not, this demonstrates the early termination issue where AST filtering"
echo "   might skip the function signature when targeting implementation lines."

echo ""
echo "🧪 Test Case 2: Complex Trait Implementation Issues"
echo "---------------------------------------------------"
echo "Searching for trait implementation methods that might be separated from their bounds"

echo ""
echo "🔍 Search for 'process_batch' method:"
$PROBE_BIN search "process_batch" "$TEST_FILE" --max-results 1

echo ""
echo "🔍 Search for 'validate_input' method:"
$PROBE_BIN search "validate_input" "$TEST_FILE" --max-results 1

echo "⚠️  Question: Do both results include the complete trait bounds and where clauses?"
echo "   The trait implementation spans lines with complex generic constraints that"
echo "   are semantically essential but might be filtered out by line range optimization."

echo ""
echo "🧪 Test Case 3: Macro-Generated Code Issues"
echo "-------------------------------------------"
echo "Testing macro expansions where generated code might span unexpected line ranges"

echo ""
echo "🔍 Search for 'create_user' (generated by macro):"
$PROBE_BIN search "create_user" "$TEST_FILE" --max-results 1

echo ""
echo "🔍 Search for 'user_service' (module generated by macro):"
$PROBE_BIN search "user_service" "$TEST_FILE" --max-results 1

echo "⚠️  Question: Do the results show the complete macro definition context?"
echo "   Macro expansions create AST nodes that might not align with simple"
echo "   line range calculations used in early termination optimization."

echo ""
echo "🧪 Test Case 4: Documentation with Code Examples"
echo "-----------------------------------------------"
echo "Testing searches that might miss documentation context due to AST filtering"

echo ""
echo "🔍 Search for 'ComplexService' (should include documentation):"
$PROBE_BIN search "ComplexService" "$TEST_FILE" --max-results 1

echo ""
echo "🔍 Search for 'process_data' method:"
$PROBE_BIN search "process_data" "$TEST_FILE" --max-results 1

echo "⚠️  Question: Do the results include the comprehensive documentation examples?"
echo "   The documentation contains important usage patterns and error handling"
echo "   examples that are semantically connected to the implementation."

echo ""
echo "🧪 Test Case 5: Generic Repository with Complex Constraints"
echo "---------------------------------------------------------"
echo "Testing code where generic constraints define essential semantic context"

echo ""
echo "🔍 Search for 'find_with_complex_criteria':"
$PROBE_BIN search "find_with_complex_criteria" "$TEST_FILE" --max-results 1

echo ""
echo "🔍 Search for 'GenericRepository':"
$PROBE_BIN search "GenericRepository" "$TEST_FILE" --max-results 1

echo "⚠️  Question: Do the results include the complete where clauses and generic bounds?"
echo "   These constraints are essential for understanding how the repository works"
echo "   but might be separated from method implementations by AST filtering."

echo ""
echo "🧪 Test Case 6: Cross-Module Dependencies"
echo "----------------------------------------"
echo "Testing nested modules where early termination might miss cross-references"

echo ""
echo "🔍 Search for 'load_user_with_permissions':"
$PROBE_BIN search "load_user_with_permissions" "$TEST_FILE" --max-results 1

echo ""
echo "🔍 Search for 'update_user_with_permission_check':"
$PROBE_BIN search "update_user_with_permission_check" "$TEST_FILE" --max-results 1

echo "⚠️  Question: Do the results show the cross-module dependencies and relationships?"
echo "   These functions are semantically connected but might be in different modules"
echo "   that could be filtered separately by optimization logic."

echo ""
echo "🔬 Advanced Testing: Debug Mode Analysis"
echo "======================================="
echo "Running searches with DEBUG=1 to see AST filtering behavior:"

echo ""
echo "🔍 Debug search for 'complex_database_query' with line range information:"
DEBUG=1 $PROBE_BIN search "complex_database_query" "$TEST_FILE" --max-results 1 2>&1 | grep -E "(DEBUG|AST|filtering|skip)" || echo "No debug output found"

echo ""
echo "🔍 Debug search for 'process_batch' to see trait implementation filtering:"
DEBUG=1 $PROBE_BIN search "process_batch" "$TEST_FILE" --max-results 1 2>&1 | grep -E "(DEBUG|AST|filtering|skip)" || echo "No debug output found"

echo ""
echo "📊 Performance vs. Accuracy Trade-off Analysis"
echo "============================================="
echo "Measuring search times with and without optimizations:"

echo ""
echo "⏱️  Normal search timing:"
time $PROBE_BIN search "ComplexService" "$TEST_FILE" --max-results 5 > /tmp/normal_search.out 2>&1

echo ""
echo "📏 Result completeness analysis:"
NORMAL_LINES=$(wc -l < /tmp/normal_search.out)
echo "Lines in normal search result: $NORMAL_LINES"

echo ""
echo "🎯 Recommendations for Testing"
echo "=============================="
echo "1. Run this script and manually inspect each result"
echo "2. Check if function signatures are complete"
echo "3. Verify that trait bounds and where clauses are included"
echo "4. Ensure macro-generated code shows full context"
echo "5. Confirm documentation examples are preserved"
echo "6. Validate cross-module dependencies are captured"

echo ""
echo "🚨 Key Questions to Answer:"
echo "=========================="
echo "• Are function signatures being truncated when searching implementation details?"
echo "• Do trait implementations include their complete generic constraints?"
echo "• Are macro expansions showing the complete generation context?"
echo "• Is documentation being included when searching for implementation methods?"
echo "• Are cross-module dependencies being preserved in search results?"

echo ""
echo "✅ Test completed. Review the output above for potential early termination issues."
echo "   Look for incomplete results, missing context, or truncated code blocks."

# Clean up temporary files
rm -f /tmp/normal_search.out